package com.mspbots.common.client;


import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.cloud.client.loadbalancer.reactive.ReactorLoadBalancerExchangeFilterFunction;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.util.Map;


@Service
public class ApiClient extends AbstractClient {

    public ApiClient(WebClient.Builder builder,
                     ReactorLoadBalancerExchangeFilterFunction lbFunction) {
        super(builder.baseUrl("http://mspbots-core")
                .filter(lbFunction)
                .build());
    }


    public <T> Mono<T> get(String server, String path, Map<String, Object> params, Class<T> responseType) {
        this.webClient = WebClient.builder().baseUrl("http://" + server).build();
        return super.get(path, params, responseType);
    }

    public <T> Mono<JsonNode> get(String server, String path) {
        this.webClient = WebClient.builder().baseUrl("http://" + server).build();
        return super.get(path);
    }


    public Mono<JsonNode> get(String server, String path, Map<String, Object> params) {
        this.webClient = WebClient.builder().baseUrl("http://" + server).build();
        return super.get(path, params);
    }

    public Mono<JsonNode> post(String server, String path, Object params) {
        this.webClient = WebClient.builder().baseUrl("http://" + server).build();
        return super.post(path, params);
    }
}
